//
//  LYQLoginSettingPasswordViewController.m
//  ZCProject
//
//  Created by Zheng on 16/11/15.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQLoginSettingPasswordViewController.h"

#import "LYQLoginDefaultCell.h"

#import "LYQLoginModel.h"

@interface LYQLoginSettingPasswordViewController () <UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIButton *enterBtn; //注册/密码重置按钮
@property (nonatomic, strong) LYQLoginModel *model;
@property (nonatomic, strong) UILabel *promptLbl; //注册提示


@end

@implementation LYQLoginSettingPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    self.listTableView.scrollEnabled = NO;
    self.listTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"login_bg2"]];
    _model = [[LYQLoginModel alloc] init];
    
    [self.view addSubview:self.enterBtn];
    
    if ([self.navigationItem.title isEqualToString:@"手机注册"]) {
        [self.view addSubview:self.promptLbl];
    };
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditingTapAction)];
    tapGR.delegate = self;
    [self.view addGestureRecognizer:tapGR];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kWScale(260));
        make.left.mas_equalTo(kWScale(42));
        make.right.mas_equalTo(-kWScale(42));
        make.height.equalTo(@(kWScale(40)));
    }];
    
    if ([self.navigationItem.title isEqualToString:@"手机注册"]) {
        [_promptLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-kWScale(65));
            make.centerX.equalTo(self.view);
        }];
    };
}

#pragma mark - load subviews
- (UIButton *)enterBtn {
    if (!_enterBtn) {
        _enterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *titleStr = @"";
        if ([self.navigationItem.title isEqualToString:@"手机注册"]) {
            titleStr = @"注册";
        } else if ([self.navigationItem.title isEqualToString:@"重置密码"]) {
            titleStr = @"重置密码";
        } else {
            titleStr = @"修改密码";
        }
        
        [_enterBtn setTitle:titleStr forState:UIControlStateNormal];
        [_enterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _enterBtn.backgroundColor = kLYQBlue;
        _enterBtn.layer.cornerRadius = kWScale(20);
        _enterBtn.titleLabel.font = kWFont(16);
        [_enterBtn addTarget:self action:@selector(enterBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _enterBtn;
}

- (UILabel *)promptLbl {
    if (!_promptLbl) {
        _promptLbl = [[UILabel alloc] init];
        _promptLbl.text = @"点击 \"注册\" 按钮 , 即表示已同意\n《旅业圈软件服务协议》";
        _promptLbl.numberOfLines = 0;
        _promptLbl.font = kWFont(13);
        _promptLbl.textColor = kLYQGray;
        _promptLbl.textAlignment = NSTextAlignmentCenter;
    }
    return _promptLbl;
}

#pragma mark - button action
#pragma mark 注册事件
- (void)enterBtnAction:(UIButton *)sender {
    
    if (_model.phoneStr.length != 11) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写正确的手机号码"];
        return;
    }
    
    if (_model.verifticationStr.length != 4) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写正确的验证码"];
        return;
    }
    
    if (_model.passwordStr.length < 6) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"密码不能小于6位"];
        return;
    }
    
    
    if ([sender.titleLabel.text isEqualToString:@"注册"]) {
        
        NSDictionary *param = @{@"mobile":_model.phoneStr,
                                @"password":[_model.passwordStr md5String],
                                @"countryid":_model.areaStr,
                                @"checkcode":_model.verifticationStr};
        
        [NetRequest PostWithRequestStyle:NetRequestHostTypeStyleDefault withAPI:@"register" param:param success:^(id response) {
            
        } error:^(NSError *error) {
            
        }];
        
        
    } else if ([sender.titleLabel.text isEqualToString:@"重置密码"]) {
        NSLog(@"重置密码");
    } else {
        //修改密码
        NSLog(@"修改密码");
    }
    
}



#pragma mark - gesture action
- (void)endEditingTapAction {
    [self.view endEditing:YES];
}

#pragma mark gesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LYQLoginDefaultCellStyle style;
    
    if (indexPath.row == 0) {
        style = LYQLoginDefaultCellStyleNation;
    } else if (indexPath.row == 1) {
        style = LYQLoginDefaultCellStyleUsername;
    } else if (indexPath.row == 2) {
        style = LYQLoginDefaultCellStyleVerification;
    } else {
        style = LYQLoginDefaultCellStylePassword2;
    }
    
    LYQLoginDefaultCell *cell = [[LYQLoginDefaultCell alloc] init];
    cell.cellStyle = style;
    cell.model = _model;
    
    return cell;
}

#pragma mark - tableview delegate
#pragma mark 分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, kWScale(10), 0, 0)];
    }
}

#pragma mark 头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kWScale(25);
}

#pragma mark 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kWScale(50);
}

#pragma mark 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        NSLog(@"选择国家或区域");
    }
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
